package com.qing.demo.netty.util;

import com.qing.demo.netty.domain.Constants;
import com.qing.demo.netty.domain.FileBurstData;
import com.qing.demo.netty.domain.FileBurstInstruct;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @author caozhipeng
 * @date 2025/7/25
 */
public class FileUtil {

    public static FileBurstData readFile(String fileUrl,Integer readPosition) {
        RandomAccessFile randomAccessFile=null;
        try {
          File file=new File(fileUrl);
//        r:只读模式 rw：读写模式
          randomAccessFile=new RandomAccessFile(file,"r");
          randomAccessFile.seek(readPosition);
          byte[] bytes=new byte[1024];
          int readSize=randomAccessFile.read(bytes);
          if(readSize<=0){
              randomAccessFile.close();
//            完成
              return new FileBurstData(Constants.FileStatus.COMPLETE);
          }

          FileBurstData fileInfo=new FileBurstData();
          fileInfo.setFileUrl(fileUrl);
          fileInfo.setFileName(file.getName());
          fileInfo.setBeginPos(readPosition);
          fileInfo.setEndPos(readPosition+readSize);
//        不足1024需要拷贝去空字节
          if(readSize<1024){
              byte[] copy=new byte[readSize];
              System.arraycopy(bytes,0,copy,0,readSize);
              fileInfo.setBytes(copy);
              fileInfo.setStatus(Constants.FileStatus.END);
          }else {
              fileInfo.setBytes(bytes);
              fileInfo.setStatus(Constants.FileStatus.CENTER);
          }
          return fileInfo;
      }catch (IOException ex){
          ex.printStackTrace();
      }finally {
            try {
                randomAccessFile.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
       return null;
    }

    public static FileBurstInstruct writeFile(String baseUrl,FileBurstData fileBurstData){
        if(Constants.FileStatus.COMPLETE== fileBurstData.getStatus()){
            return new FileBurstInstruct(Constants.FileStatus.COMPLETE);
        }
        File file=new File(baseUrl+"/"+fileBurstData.getFileName());
        try {
            RandomAccessFile randomAccessFile=new RandomAccessFile(file,"rw");
//            移动文件记录指针位置
            randomAccessFile.seek(fileBurstData.getBeginPos());
//            调用seek方法，将文件记录指针移动到beginPos位置
            randomAccessFile.write(fileBurstData.getBytes());
            randomAccessFile.close();

            if(Constants.FileStatus.END==fileBurstData.getStatus()){
                return new FileBurstInstruct(Constants.FileStatus.COMPLETE);
            }
//            文件分片传输指令
            FileBurstInstruct fileBurstInstruct=new FileBurstInstruct();
            fileBurstInstruct.setStatus(Constants.FileStatus.CENTER);
//            客户端文件地址
            fileBurstInstruct.setClientFileUrl(fileBurstData.getFileUrl());
//            读取位置
            fileBurstInstruct.setReadPosition(fileBurstData.getEndPos()+1);
            return fileBurstInstruct;

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
